package com.sskj.level.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.level.bean.ProductDataBean;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.HolderFragment;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class HolderFragmentPresenter extends BasePresenter<HolderFragment> {


    public void getTradeList(String page, String pageSize, String stockCode, UserData userData) {
        if (userData == null) {
            mView.updateUI(new ArrayList<>());
            return;
        }
        httpService.getChiCang(page, pageSize, stockCode)
                .execute(new CallBackOption<HttpData<PageBean<TradeListBean>>>() {
                    @Override
                    public void onError(Response<HttpData<PageBean<TradeListBean>>> response) {
                        super.onError(response);
                        mView.updateUI(new ArrayList<>());
                    }
                }
                        .unLoadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                mView.updateUI(httpData.getData().getList());
                            }

                        }));
    }

    public Flowable<List<TradeListBean>> getTradeListFlow(String page, String pageSize, String stockCode, UserData userData) {
        if (userData == null) {
            return Flowable.fromIterable(new ArrayList<TradeListBean>())
                    .toList()
                    .toFlowable();
        }
        return httpService.getChiCang(page, pageSize, stockCode)
                .converter(new JsonConverter<HttpData<PageBean<TradeListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturn(throwable -> new ArrayList<>())
                ;

    }

    /**
     * 一键平仓
     */
    public void levelAllPc(String stockCode) {
        httpService.levelAllPc(stockCode)
                .execute(new CallBackOption<HttpData>() {
                }
                        .unLoadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                ToastUtil.showShort(httpData.getMsg());
                                LiveDataBus.get().with(RxBusCode.LEVEL_ALL_PC).postValue(1);
                            }
                        }));
    }

    public void pingCang(String orderId, String num) {
        httpService.pingCang(orderId, num)
                .execute(new CallBackOption<HttpData>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                mView.pingCangSuccess();
                            }
                        }));
    }

    public void setStop(String orderId, String stopProfitPrice, String stopLossPrice) {
        httpService.setStop(orderId, stopProfitPrice, stopLossPrice)
                .execute(new CallBackOption<HttpData>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> {
                            ToastUtil.showShort(httpData.getMsg());
                            if (httpData.getStatus() == HttpConfig.OK) {
                                mView.refresh();
                            }
                        }));
    }

    public void getSet(String stockCode) {
        httpService.getProductData(stockCode)
                .execute(new CallBackOption<HttpData<ProductDataBean>>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                String minLossPrice = httpData.getData().getMinStopLoss();
                                String minWinPrice = httpData.getData().getMinStopProfit();
                                mView.setStop(minWinPrice, minLossPrice);
                            }
                        }));
    }
}
